(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.siteCalendar.createRejectController', createRejectController);
	createRejectController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'siteCalendarService' ];
	function createRejectController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, siteCalendarService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.siteCalendar = {};
		
		function diffArray(arr1, arr2) {
			if (arr1 != null)  {
			if (arr1.length == 1 && arr2 == null) {
				return arr1[0];
			}
			for (var i = 0; i < arr1.length; i++) {
				var findFlag = false;
				for (var j = 0; j < arr2.length; j++) {
					if (arr1[i] == arr2[j]) {
						findFlag = true;
						break;
					}
				}
				if (!findFlag) {
					return arr1[i];
				}
			}
			}
			return false;
		}
		
		function getDateIndex(date, arr) {
			for (var index = 0; index < arr.length; index++) {
				var eachDate = arr[index];
				if (eachDate.getYear() == date.getYear() && eachDate.getMonth() == date.getMonth() && eachDate.getDate() == date.getDate()) {
					return index;
				}
			}
			return -1;
		}
		
		
		ctrl.init = function() {
			siteCalendarService.initOptions($scope, ctrl);
			authService.createReject(params).then(function(response) {
				ctrl.siteCalendar = response.data.model;
				ctrl.siteCalendar.taskId = response.data.task.taskId;
				$scope.calendarYear = ctrl.siteCalendar.calendarDay.year;
				
				$scope.$watch('vm.siteCalendar.weekendDays', function(newValue, oldValue) {
					if (ctrl.siteCalendar.calendarDay) {
						var daystrs;
						var calendarInfo = ctrl.siteCalendar.calendarDay.calendarInfo;
						if ((newValue != null && oldValue!= null && newValue.length > oldValue.length)
								|| (newValue != null && newValue.length == 1 && oldValue == null)) {
								var newWeekday = diffArray(newValue, oldValue);
								if (newWeekday == 'MON') {
									daystrs = calendarInfo[5 ].split(',');
								} else if (newWeekday == 'TUE') {
									daystrs = calendarInfo[7 ].split(',');
								} else if (newWeekday == 'WED') {
									daystrs = calendarInfo[9 ].split(',');
								} else if (newWeekday == 'THU') {
									daystrs = calendarInfo[11 ].split(',');
								} else if (newWeekday == 'FRI') {
									daystrs = calendarInfo[13 ].split(',');
								} else if (newWeekday == 'SAT') {
									daystrs = calendarInfo[1 ].split(',');
								} else if (newWeekday == 'SUN') {
									daystrs = calendarInfo[3 ].split(',');
								}
								for (var index in daystrs) {
									var date = new Date(Date.parse(daystrs[index].replace(/-/g,   "/")))
									if (getDateIndex(date, ctrl.siteCalendar.holidays) == -1) {
										ctrl.siteCalendar.holidays.push(date);
									}
									
								}
							} else {
								var rmvWeekday = diffArray(oldValue, newValue);
								if (rmvWeekday == 'MON') {
									daystrs = calendarInfo[5 ].split(',');
								} else if (rmvWeekday == 'TUE') {
									daystrs = calendarInfo[7 ].split(',');
								} else if (rmvWeekday == 'WED') {
									daystrs = calendarInfo[9 ].split(',');
								} else if (rmvWeekday == 'THU') {
									daystrs = calendarInfo[11 ].split(',');
								} else if (rmvWeekday == 'FRI') {
									daystrs = calendarInfo[13 ].split(',');
								} else if (rmvWeekday == 'SAT') {
									daystrs = calendarInfo[1 ].split(',');
								} else if (rmvWeekday == 'SUN') {
									daystrs = calendarInfo[3 ].split(',');
								}
								for (var index in daystrs) {
									var date = new Date(Date.parse(daystrs[index].replace(/-/g,   "/")))
									var index = getDateIndex(date, ctrl.siteCalendar.holidays);
									if (index > -1) {
										ctrl.siteCalendar.holidays.splice(index, 1);
									}
									
								}
							}
						}
				}, true);
			});
		};

		ctrl.createRejectSave = function() {
			return authService.createRejectSave(ctrl.siteCalendar).then(saveSuccessful, saveFailed);
		};

		ctrl.createRejectSubmit = function() {
			return authService.createRejectSubmit(ctrl.siteCalendar).then(submitSuccessful, submitFailed);
		};
		
		ctrl.createRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.siteCalendar.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createRejectCancel(ctrl.siteCalendar).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.sitecalendar.view',
					'state' : 'cda.siteCalendar.view',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.merge(ctrl.siteCalendar, response.data.model);
			} else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.sitecalendar.create',
					'state' : 'cda.siteCalendar.create',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}		
	}
})(angular);